//
//  AddNewContactViewController.m
//  Paparazzi
//
//  Created by Eric Williams on 11/6/10.
//  Copyright 2010 Eric Williams. All rights reserved.
//

#import "AddNewContactViewController.h"
#import "FlickrFetcher.h"
#import "Person.h"

@implementation AddNewContactViewController

- (IBAction) cancelButtonPushed {
	[self dismissModalViewControllerAnimated: YES];
}

- (IBAction) doneButtonPushed {
	
	// Check to see if the new contact already exists in Core Data or is empty
	FlickrFetcher *flickrFetcher = [FlickrFetcher sharedInstance];
	NSPredicate *matchUserPredicate = [NSPredicate predicateWithFormat: @"name MATCHES %@", nameTextField.text];
	NSArray *fetchedPeople = [flickrFetcher fetchManagedObjectsForEntity: @"Person" withPredicate: matchUserPredicate];
	
	// If the contact already exists, inform the user
	if (fetchedPeople.count != 0) {
		duplicateLabel1.text = [NSString stringWithFormat: @"The user '%@'", nameTextField.text];
		duplicateLabel1.hidden = NO;
		duplicateLabel2.hidden = NO;
		
		duplicateImageView.image = [UIImage imageNamed: @"DuplicateContacts.png"];
		duplicateImageView.hidden = NO;
	}
	
	// If the contact is blank, inform the user
	else if ([nameTextField.text length] == 0) {
		duplicateLabel1.text = [NSString stringWithString: @"The user field was blank"];
		duplicateLabel1.hidden = NO;
		duplicateLabel2.hidden = YES;
		
		duplicateImageView.image = [UIImage imageNamed: @"NoContacts.png"];
		duplicateImageView.hidden = NO;
	}
	
	// Otherwise, add a new managed Person to the DB and fill it with images
	else {
		// Create the Person object and insert it into the DB
		Person *person = [NSEntityDescription insertNewObjectForEntityForName: @"Person" inManagedObjectContext: flickrFetcher.managedObjectContext];
		
		duplicateLabel1.hidden = YES;
		duplicateLabel2.hidden = YES;
		duplicateImageView.hidden = YES;
		
		// Setup the Person
		person.name = nameTextField.text;
		
		// TO DO
		// Get his photos
		// ...
		
		// TO DO
		// Create the Photo objects and insert them into the DB
		// ...
		
		// Save the changes to the DB
		[flickrFetcher.managedObjectContext save: nil];
		
		// Return to the prior view
		[self dismissModalViewControllerAnimated: YES];
	}
}

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

	[nameTextField becomeFirstResponder];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@synthesize cancelButton;
@synthesize nameTextField;
@synthesize duplicateLabel1;
@synthesize duplicateLabel2;
@synthesize duplicateImageView;
@end
